package com.service;
import com.dao.AsFunctionDao;
import com.entity.FunctionPremissionTreeGrid;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

@Service
public class AsFuntionService
{
    @Resource
    private AsFunctionDao asFunctionDao;

    //根据roleId查询 返回指定用户的所有可用子权限名称
    public ArrayList<Map<String,Object>> returnAllAvaliableFunctionNameByRoleId( Integer roleId )
    {
        return asFunctionDao.getAvaliableFunctionNameByRoleId( roleId );
    }

    //查询 返回所有可用根权限名称
    public ArrayList<Map<String,Object>> returnAllAvaliableRootFunctionName()
    {
        return asFunctionDao.getAllAvaliableRootFunctionName();
    }

    //查询 组合成根权限的treegrid
    public ArrayList<FunctionPremissionTreeGrid> returnAllRootTreeGrid( Integer parentId )
    {
        ArrayList<FunctionPremissionTreeGrid> list = new ArrayList<>();
        ArrayList<Map<String, Object>> resultList = asFunctionDao.getAllRootFunctionTreeGrid( parentId );
        for ( Map<String, Object> x : resultList )
        {
            FunctionPremissionTreeGrid afp = new FunctionPremissionTreeGrid();
            afp.setId( (Integer)x.get( "id" ) );
            afp.setName( (String)x.get( "functionName" ) );
            afp.setParentId( (Integer)x.get( "parentId" ) );
            afp.setFuncUrl( (String)x.get( "funcUrl" ) );
            afp.setCreationTime( (Date)x.get( "creationTime" ) );
            //树表格中的根权限默认为关闭
            afp.setState( "closed" );
            //MySql将tinyint(1)转换为java的boolean类型
            afp.setIsStart( (Boolean)x.get( "isStart" ) );
            afp.setPremission( null );
            list.add( afp );
        }

        return list;
    }

    //查询 返回当前用户的某根权限下的子权限treegrid
    public ArrayList<FunctionPremissionTreeGrid> returnSubFunctionTreeGridByRoleIdAndParentId( Integer parentId , Integer roleId )
    {
        ArrayList<FunctionPremissionTreeGrid> list = new ArrayList<>();
        //首先查询管理员具有的该项根权限下的子权限
        ArrayList<Map<String,Object>> adminSubFunctionList =
                asFunctionDao.getSubFunctionTreeGridByRoleIdAndParentId( parentId , 1 );
        //System.out.println(adminSubFunctionList);
        //查询指定用户具有的该项根权限下的子权限
        ArrayList<Map<String,Object>> userSubFunctionList =
                asFunctionDao.getSubFunctionTreeGridByRoleIdAndParentId( parentId , roleId );
        //System.out.println( userSubFunctionList);
        for ( Map<String, Object> x : adminSubFunctionList )
        {
            FunctionPremissionTreeGrid afp = new FunctionPremissionTreeGrid();
            afp.setId( (Integer)x.get( "id" ) );
            afp.setName( (String)x.get( "functionName" ) );
            afp.setParentId( (Integer)x.get( "parentId" ) );
            afp.setFuncUrl( (String)x.get( "funcUrl" ) );
            afp.setCreationTime( (Date)x.get( "creationTime" ) );
            //根据当前权限标题下属是否存在子权限来设置treegrid呈现文件夹还是文件图标
            afp.setState( ( (Integer)x.get( "isLeaf" ) > 0 )?"closed":"open" );
            //MySql将tinyint(1)转换为java的boolean类型
            afp.setIsStart( (Boolean)x.get( "isStart" ) );
            //设置是否授权
            afp.setPremission( userSubFunctionList.contains( x ) );
            list.add( afp );
        }

        return list;
    }
}
